'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  // 获取今天的日期字符串：YYYY-MM-DD
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const date = String(now.getDate()).padStart(2, '0');
  const todayStr = `${year}-${month}-${date}`; // 例：2025-04-16

  try {
    // 获取 appoint_schedule 中今日未结束的记录
    const scheduleRes = await db.collection('appoint_schedule')
      .where({
        day: todayStr,
      })
      .count();

    // 获取 appoint_record 中今日已结束的记录
    const recordRes = await db.collection('appoint_records')
      .where({
        day: todayStr
      })
      .count();

    const totalCount = scheduleRes.total + recordRes.total;

    return {
      code: 0,
      msg: '获取成功',
      result: {
        count: totalCount,
        notEndedCount: scheduleRes.total,
        endedCount: recordRes.total
      }
    };
  } catch (err) {
    return {
      code: 1,
      msg: '获取失败',
      error: err.message
    };
  }
};
